package com.nikkerball.apps.darkhorse.rules;

import com.nikkerball.apps.darkhorse.model.base.NameObject;
import com.nikkerball.apps.darkhorse.model.security.SecurityContext;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: Document class
 *
 * @author marius (created on 2010-12-05)
 */
public class RuleEngineImpl implements RuleEngine {

    // ----- instance fields -----

    private List<Rule> rules;

    // ----- RuleEngine -----

    public boolean isRuleSatisfied(SecurityContext securityContext, NameObject nameObject, RuleName ruleName) {
        boolean ruleSatisfied = false;
        for (Rule rule : this.rules) {
            if (ruleName.equals(rule.getName())) {
                ruleSatisfied = rule.isSatisfied(securityContext, nameObject);
                if (ruleSatisfied) {
                    return true;
                }
            }
        }
        return ruleSatisfied;
    }

    public void addRule(Rule rule) {
        if (null == this.rules) {
            this.rules = new ArrayList<Rule>();
        }
        this.rules.add(rule);
    }

    // ----- public methods -----

    public List<Rule> getRules() {
        return this.rules;
    }

    public void setRules(List<Rule> rules) {
        this.rules = rules;
    }
}
